#include "ObjectManager.h"

namespace XGE
{
    bool ObjectManager::init()
    {
        bInit = true;
        return true;
    }

    bool ObjectManager::_update()
    {
        if(!elements.empty())
        {
            XGE::ElementMap::iterator i = elements.begin();
            while(i != elements.end())
            {
                if((*i).second->_update())
                {
                    XGE::ElementMap::iterator j = i;
                    i++;

                    (*j).second->setActive(false);
                    delete (*j).second;
                    elements.erase(j);
                }
                else
                {
                    i++;
                }
            }
        }

        return true;
    }

    bool ObjectManager::registerComponentType(XGE::ObjectComponent* nOC)
    {
        if(nOC)
        {
            if(!ObjectComponentTypes[nOC->getTypeName()])
            {
                ObjectComponentTypes[nOC->getTypeName()] = nOC;
                return true;
            }
            else
            {
                throw(Ogre::Exception(Ogre::Exception::ERR_DUPLICATE_ITEM, "Unable to register \"" + nOC->getName() + "\" to the Object Manager because an Object Component of the same name already exists!", "ObjectManager.cpp"));
            }
        }

        throw(Ogre::Exception(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to register an Object Component to the Object Manager because it is invalid!", "ObjectManager.cpp"));

        return false;
    }

    XGE::ObjectComponent* ObjectManager::addComponent(XGE::Object* nObj, const Ogre::String& nOCName, const Ogre::String& nOCTypeName)
    {
        if(ObjectComponentTypes[nOCTypeName])
        {
            if(nObj)
            {
                ObjectComponentMap oc = nObj->getComponents();

                if(!oc[nOCName])
                {
                    XGE::ObjectComponent* newOC = ObjectComponentTypes[nOCTypeName]->_clone(nOCName);
                    oc[nOCName] = newOC;

                    return newOC;
                }
                else
                {
                    throw(Ogre::Exception(Ogre::Exception::ERR_DUPLICATE_ITEM, "Unable to add \"" + nOCName + "\" of type \"" + nOCTypeName + "\" to \"" + nObj->getName() + "\" because an Object Component with that name already exists!", "ObjectManager.cpp"));
                }
            }
            else
            {
                throw(Ogre::Exception(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to add \"" + nOCName + "\" of type \"" + nOCTypeName + "\" to an Object because the Object pointer is invalid!", "ObjectManager.cpp"));
            }
        }


        return 0;
    }

    XGE::ObjectComponent* ObjectManager::getComponent(XGE::Object* nObj, const Ogre::String& nOCName)
    {
        if(nObj)
        {
            ObjectComponentMap oc = nObj->getComponents();

            if(oc[nOCName])
            {
                return oc[nOCName];
            }
            else
            {
                throw(Ogre::Exception(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Unable to get Object Component \"" + nOCName + "\" from Object \"" + nObj->getName() + "\" because it does not exist!", "ObjectManager.cpp"));
            }
        }

        throw(Ogre::Exception(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to get Object Component \"" + nOCName + "\" from an Object because the Object pointer is invalid!", "ObjectManager.cpp"));
        return 0;
    }

    void ObjectManager::delComponent(XGE::Object* nObj, XGE::ObjectComponent* nOC)
    {
        if(nObj)
        {
            ObjectComponentMap oc = nObj->getComponents();
            ObjectComponentMap::iterator i;
            for(i = oc.begin(); i != oc.end(); ++i)
            {
                if(nOC == (*i).second)
                {
                    delete (*i).second;
                    oc.erase(i);
                    return;
                }
            }

            throw(Ogre::Exception(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to delete an Object Component from an Object because the Object Component does not exist!", "ObjectManager.h"));
        }
        else
        {
            throw(Ogre::Exception(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to delete an Object Component from an Object because the Object pointer is invalid!", "ObjectManager.h"));
        }
    }

}
